package com.fjb.service.product.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.entity.JsonTotalResult;
import com.fjb.common.entity.PageQuery;
import com.fjb.common.enums.product.DealType;
import com.fjb.common.enums.product.ProductStatus;
import com.fjb.common.enums.product.ProductType;
import com.fjb.common.utils.SerialNumberUtils;
import com.fjb.mapper.product.ProductAttributeMapper;
import com.fjb.mapper.product.ProductAttributeSkuMapper;
import com.fjb.mapper.product.ProductFilesMapper;
import com.fjb.mapper.product.ProductInfoMapper;
import com.fjb.pojo.product.ProductAttribute;
import com.fjb.pojo.product.ProductAttributeSku;
import com.fjb.pojo.product.ProductFiles;
import com.fjb.pojo.product.ProductInfo;
import com.fjb.pojo.product.query.ProductInfoQuery;
import com.fjb.pojo.product.vo.ProductInfoPageVo;
import com.fjb.service.product.ProductInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * <p>
 * 产品信息 服务实现类
 * </p>
 *
 * @author hemiao
 * @since 2019-12-22
 */
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements ProductInfoService {
	
	@Autowired
	private ProductInfoMapper productInfoMapper;
	@Autowired
	private ProductAttributeMapper productAttributeMapper;
	@Autowired
	private ProductAttributeSkuMapper productAttributeSkuMapper;
	@Autowired
	private ProductFilesMapper productFilesMapper;
	
	@Override
	public JsonTotalResult<List<ProductInfoPageVo>> selectPageList(ProductInfoQuery productInfoQuery, PageQuery pageQuery) {
		PageHelper.startPage(pageQuery.getCurrentPage(), pageQuery.getPageSize());
		List<ProductInfoPageVo> infoList = productInfoMapper.selectPageList(productInfoQuery);
		PageInfo<ProductInfoPageVo> pageInfo = new PageInfo<ProductInfoPageVo>(infoList);
		return new JsonTotalResult<List<ProductInfoPageVo>>(infoList, HttpCode.SUCCESS, pageInfo.getTotal());
	}

	@Override
	public Boolean updateInfo(ProductInfo productInfo) {
		// TODO Auto-generated method stub
		return true;
	}
	
	@Transactional
	@Override
	public JsonResult<Integer> addInfo(ProductInfo productInfo, List<ProductAttribute> attributeLiat,
			List<ProductAttributeSku> attributeSkuList,List<ProductFiles> filesList) {
		Integer mainUserId = productInfo.getMainUserId();
		Integer createUserId = productInfo.getCreateUserId();
		LocalDateTime dateTime = LocalDateTime.now();
		String productNumber = SerialNumberUtils.createNumber(SerialNumberUtils.PRODUCT_INFO, mainUserId);
		productInfo.setProductNumber(productNumber);
		productInfo.setCreateTime(dateTime);
		productInfo.setProductStatus(ProductStatus.STATUS_2.getCode());
		productInfo.setDealType(DealType.TYPE_1.getCode());
		productInfo.setProductType(ProductType.STATUS_1.getCode());
		int i = productInfoMapper.insert(productInfo);
		Integer productId = productInfo.getId();
		
		if(attributeLiat.size()>0) {
			// 批量添加属性
			for (ProductAttribute attr : attributeLiat) {
				attr.setMainUserId(mainUserId);
				attr.setCreateUserId(createUserId);
				attr.setProductId(productId);
				attr.setCreateTime(dateTime);
			}	
			productAttributeMapper.addBatch(attributeLiat);
		}
		
		if(attributeSkuList.size()>0) {
			// 批量添加属性sku
			for (ProductAttributeSku sku : attributeSkuList) {
				sku.setMainUserId(mainUserId);
				sku.setCreateUserId(createUserId);
				sku.setProductId(productId);
				sku.setCreateTime(dateTime);
			}
			productAttributeSkuMapper.addBatch(attributeSkuList);
		}
		
		if(filesList.size()>0) {
			for (ProductFiles file : filesList) {
				file.setMainUserId(mainUserId);
				file.setCreateUserId(createUserId);
				file.setProductId(productId);
				file.setCreateTime(dateTime);
			}
			productFilesMapper.addBatch(filesList);
		}
		return new JsonResult<Integer>(i, HttpCode.SUCCESS);
	}

}
